package com.huika.renrench.control.login.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.huika.lib.validation.EditTextValidator;
import com.huika.lib.validation.ValidationModel;
import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.login.event.LoginEvent;
import com.huika.renrench.control.login.validations.PassWordValidation;
import com.huika.renrench.utils.LoginUtils;
import com.huika.renrench.utils.PreferencesHelper;
import com.huika.renrench.view.TitleBarHelper;
import com.huika.renrench.view.clearedit.ClearableEditText;

import de.greenrobot.event.EventBus;

/**
 * @ClassName: LoginActivity
 * @说明:登陆页面 bote8339 12345678
 * @author zwy
 * @date 2015年6月4日 下午7:24:46
 */
public class LoginActivity extends BaseAct {
	private ClearableEditText etPhone;
	private ClearableEditText etPsw;
	private Button btnLogin;
	private Button btnRegister;
	private Button btnFind;
	private EditTextValidator editTextValidator;
	private String phone;

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_login);
		EventBus.getDefault().register(this);
	}

	@Override
	protected void initWidget() {
		TitleBarHelper titleBarHelper = new TitleBarHelper(this, -1, -1, R.string.title_login);
		titleBarHelper.setLeftMsg(R.string.left_back);
		etPhone = (ClearableEditText) findViewById(R.id.et_phone_al);
		etPsw = (ClearableEditText) findViewById(R.id.et_psw_al);
		btnLogin = (Button) findViewById(R.id.btn_login_al);
		btnRegister = (Button) findViewById(R.id.btn_register_al);
		btnFind = (Button) findViewById(R.id.btn_findpsw_al);

		etPsw.addTextChangedListener(new MyPassWordWatcher(etPsw));

		// editTextValidator = new EditTextValidator(this).add(new
		// ValidationModel(etPsw, new PassWordValidation())).execute();
		initListener();
		initEdit();
	}

	private void initListener() {
		btnLogin.setOnClickListener(this);
		btnRegister.setOnClickListener(this);
		btnFind.setOnClickListener(this);
	}

	class MyPassWordWatcher implements TextWatcher {
		private EditText editText;
		private String tempStr;

		public MyPassWordWatcher(EditText editText) {
			this.editText = editText;
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (tempStr.indexOf(" ") != -1) {
				editText.setText(tempStr.trim());
				editText.setSelection(tempStr.length());
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (s.toString().equals(" ")) {
				editText.setText("");
			} else {
				tempStr = s.toString();
			}
		}

	}

	/**
	 * @说明:初始化账号
	 * @author: zwy
	 */
	private void initEdit() {
		String userName = PreferencesHelper.getString(PreferencesHelper.KEY_LOGIN_NAME);
		if (userName != null && !TextUtils.isEmpty(userName)) {
			etPhone.setText(userName);
		}
	}

	@Override
	public void widgetClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login_al:
			login();
			break;
		case R.id.btn_register_al:
			showActivity(LoginActivity.this, RegisterActivity.class);
			break;
		case R.id.btn_findpsw_al:
			showActivity(LoginActivity.this, FindPwdActivity.class);
			break;

		default:
			break;
		}
	}

	private void login() {
		phone = getInputStr(etPhone);
		if (phone == null || TextUtils.isEmpty(phone)) {
			showToastMsg("请输入手机号/惠信号");
			return;
		}
		String pwd = getInputStr(etPsw);
		if (pwd == null || TextUtils.isEmpty(pwd)) {
			showToastMsg("请输入密码");
			return;
		}
		showLoadingDialog("");
		LoginUtils loginUtils = new LoginUtils(this);
		loginUtils.login(phone,pwd);
	}

	public void onEventMainThread(LoginEvent loginEvent) {
		dismissLoadingDialog();
		if (loginEvent.getLoginStatus() == 1) {
			this.finish();
		}else{
			showToastMsg(loginEvent.getMsg());
		}
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

}
